package sd.zhr.moodcapsule.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import sd.zhr.moodcapsule.common.WrapMapper;
import sd.zhr.moodcapsule.common.Wrapper;
import sd.zhr.moodcapsule.pojo.User;
import sd.zhr.moodcapsule.service.UserServiceImpl;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;

    @GetMapping("/getAll")
    @Secured({"ROLE_ADMIN"})
    public Wrapper<List<User>> getAllUsers() {
        List<User> lsituser=userService.getAllUsers();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,lsituser);
    }
    @PutMapping("/update")
    @Secured({"ROLE_ADMIN"})
    public Wrapper<Boolean> updateUser(@RequestBody User user) {
        int i=userService.updateUser(user);
        Boolean result=false;
        if(i>0){
            result=true;
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
    }
    @DeleteMapping("/delete/{id}")
    @Secured({"ROLE_ADMIN"})
    public Wrapper<Boolean> deleteUser(@PathVariable Integer id) {
        int i=userService.deleteUser(id);
        Boolean result=false;
        if(i>0){
            result=true;
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
    }
    @PutMapping("/updateValid")
    @Secured({"ROLE_ADMIN"})
    public Wrapper<Boolean> updateValid(@RequestBody User user) {
        int i=userService.updateValid(user);
        Boolean result=false;
        if(i>0){
            result=true;
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
    }
}
